import qs, {stringify} from "qs";
import axios from "../axios";
import {ArticleFormType, DictBodyType, LiteratureFormType, LoginType, UserBodyType} from "../util/publicType";

// 登录
export const login = (data: LoginType) => axios.post("/login", data);

// 用户列表
export const userList = (page: number, size: number, username: string | null = null) => {
    const query = {
        page,
        size,
        filters: {
            username
        }
    }
    return axios.get(`/user-list?${qs.stringify(query, {skipNulls: true})}`);
}

//权限列表
export const jurisdictionList = () => axios.get("/jurisdiction");

//创建用户
export const createUser = (data: UserBodyType) => axios.post("/user", data);

//编辑用户
export const editUser = (data: UserBodyType, id: string) => axios.put(`/user/${id}`, data);

//删除用户
export const delUser = (id: string) => axios.delete(`/user/${id}`);

//个人信息
export const info = () => axios.get("/info");

//分类接口
export const dictCategory = () => axios.get("/field-category-list");

//字典列表
export const dictList = (page: number, size: number, type_id: string | null = null, name: string | null = null) => {
    const query = {
        page,
        size,
        filters: {
            type_id,
            name
        }
    };
    return axios.get(`/dict-list?${qs.stringify(query, {skipNulls: true})}`);
}

//删除字典分类
export const delDict = (id: string) => axios.delete(`/dict/${id}`);

//编辑字典
export const editDict = (data: DictBodyType, id: string) => axios.put(`/dict/${id}`, data);

//添加字典
export const addDict = (data: DictBodyType) => axios.post(`/dict`, data);

//查询字典下的分类
export const dictDetail = (id: string) => axios.get(`/dict-category/${id}`);

//查询文献
export const literatureList = (page: number, size: number, name: string | null = null, grounding: number | null = null, category_id: string) => {
    const query = {
        page,
        size,
        filters: {
            name,
            grounding,
            category_id
        }
    }
    return axios.get(`/literature?${qs.stringify(query, {skipNulls: true})}`)
}

//更新是否推荐
export const literatureUpdateRecommend = (id: string, data: { recommend: number }) => axios.put(`/literature/update-recommend/${id}`, data);

//是否上架
export const literatureUpdateGrounding = (id: string, data: { grounding: number }) => axios.put(`/literature/update-grounding/${id}`, data);

//新建文献
export const createdLiterature = (data: LiteratureFormType) => axios.post("/literature", data);

//删除文献
export const delLiterature = (id: string) => axios.delete(`/literature/${id}`);

//文献详情
export const literatureDetail = (id: string) => axios.get(`/literature-detail/${id}`);

//更新文獻
export const updateLiterature = (id: string, data: LiteratureFormType) => axios.put(`/literature/${id}`, data);

//文章列表
export const articleList = (page: number, size: number, name: string | null = null, grounding: number | null = null, category_id: string) => {
    const query = {
        page,
        size,
        filters: {
            name,
            grounding,
            category_id
        }
    }
    return axios.get(`/article?${qs.stringify(query, {skipNulls: true})}`)
}
// 是否上架
export const articleUpdateGrounding = (id: string, data: { grounding: number }) => axios.put(`/article/update-grounding/${id}`, data);

//是否推荐
export const articleUpdateRecommend = (id: string, data: { recommend: number }) => axios.put(`/article/update-recommend/${id}`, data);

//删除文章
export const delArticle = (id: string) => axios.delete(`/article/${id}`);

//文章详情
export const articleDetail = (id: string) => axios.get(`/article-detail/${id}`);

//新增文章
export const addArticle = (data: ArticleFormType) => axios.post("/article/", data);

//编辑文章
export const editArticle = (id: string, data: ArticleFormType) => axios.put(`article/${id}`, data);

//文献关联列表
export const relationLiteratureList = (id: string, page: number, size: number, name: string | null = null, region_id: number | null = null) => {
    const query = {
        page,
        size,
        filters: {
            name,
            region_id
        }
    }
    return axios.get(`/relation-literature-list/${id}?${qs.stringify(query, {skipNulls: true})}`);
}

//文章关联列表
export const relationArticleList = (id: string, page: number, size: number, name: string | null = null, region_id: number | null = null) => {
    const query = {
        page,
        size,
        filters: {
            name,
            region_id,
        }
    }
    return axios.get(`/relation-article-list/${id}?${qs.stringify(query, {skipNulls: true})}`);
}

//取消文献关联
export const cancelRelationLiterature = (id: string) => axios.put(`/cancel-relation-literature/${id}`);

//添加文献关联
export const relationLiterature = (id: string, data: { literature_id: string }) => axios.post(`/relation-literature/${id}`, data);

//取消文章关联
export const cancelRelationArticle = (id: string) => axios.put(`/cancel-relation-article/${id}`);

//添加文章关联
export const relationArticle = (id: string, data: { article_id: string }) => axios.post(`/relation-article/${id}`, data);

//区域详情
export const regionDetail = (id: string) => axios.get(`/region-detail/${id}`);

//更新区域
export const updateRegion = (id: string, data: { images: string[], videos: string[], content: string }) => axios.put(`/region-update/${id}`, data);

// 文献排名
export const literatureRanking = () => axios.get("/literature-ranking");

//文章排名
export const articleRanking = () => axios.get("/article-ranking");

//区域排名
export const regionRanking = () => axios.get("/region-ranking");